package bookManage;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 借阅记录
 * 
 * @author 伟❤志
 *
 */
public class BorrowModel {

	private int cardId;// 借阅卡id
	private int bookId;// 图书id
	private String bookName;// 图书名
	private Date date;// 借阅时间

	public String getBookName() {
		return bookName;
	}

	public void setBookName(String bookName) {
		this.bookName = bookName;
	}

	public String getDate() {
		SimpleDateFormat sdf = new SimpleDateFormat();
		sdf.applyPattern("yyyy-MM-dd HH:mm:ss");// 格式化时间
		return sdf.format(date);
	}

	public void setDate() {
		Date date = new Date();
		this.date = date;
	}

	public int getCardId() {
		return cardId;
	}

	public void setCardId() {
		int cardId = Card.userId;
		this.cardId = cardId;
	}

	public int getBookId() {
		return bookId;
	}

	public void setBookId(int bookId) {
		this.bookId = bookId;
	}

	/**
	 * 查询全部借阅记录
	 */
	public void selectAll() {
		for (BorrowModel borrowModel : BookTest.borrows) {
			borrowInfo(borrowModel);

		}
	}

	/**
	 * 查询个人借阅记录
	 */
	public void selectByUser() {
		for (BorrowModel borrowModel : BookTest.borrows) {
			if (borrowModel.getCardId() == Card.userId) {// 当前登录用户的借阅卡id
				borrowInfo(borrowModel);
			}
		}
	}

	/**
	 * 查询图书借阅记录
	 */
	public void selectByBook(String bookName) {
		
		for (BorrowModel borrowModel : BookTest.borrows) {
			if (borrowModel.getBookName().equals(bookName)) {
				borrowInfo(borrowModel);
			}
		}
	}

	/**
	 * 借阅记录详情
	 * 
	 * @param borrowModel
	 */
	public void borrowInfo(BorrowModel borrowModel) {

		System.out.println("=================");
		System.out.println("借阅卡id：" + borrowModel.getCardId());
		System.out.println("图书编号：" + borrowModel.getBookId());
		System.out.println("图书名：" + borrowModel.getBookName());
		System.out.println("借阅时间：" + borrowModel.getDate());
		System.out.println("=================");
	}

	/**
	 * 增加借阅记录
	 * 
	 * @param cardId
	 * @param bookId
	 */
	public void addBorrow(int cardId) {
		BorrowModel borrowModel = new BorrowModel();

		BookModel bookModel = new BookModel();
		BookModel book = bookModel.selectById(bookId);// 根据图书id得到图书相关信息

		borrowModel.setBookId(bookId);
		borrowModel.setCardId();
		borrowModel.setBookName(book.getName());
		borrowModel.setDate();

		BookTest.borrows.add(borrowModel);
		
		bookModel.selectById(bookId).setRecordNum();//将图书借阅数量+1
	}

}
